<template>
    <!-- 主要内容 -->
    <main class="main-content">
      <div class="container">
        <!-- 欢迎横幅 -->
        <section class="welcome-banner" 
        :style="{'background-image': `url('${getImageUrl(about.aBanner)}')`,
        'background-repeat':'no-repeat'}">
          <h2>{{ about.aTitle }}</h2>
          <p>{{ about.aSubtitle }}</p>
        </section>

    <!-- 搜索和筛选区域 -->
    <div class="filter-section">
      <div class="container">
        <div class="search-filter-container">
          <!-- 搜索框 -->
          <div class="search-box">
            <input 
              type="text" 
              v-model="searchQuery" 
              @keyup.enter="router.push({path:'/articles',query:{title:searchQuery}})"
              placeholder="搜索文章..."
              class="search-input"
            >
          </div>
        </div>
      </div>
    </div>

        <!-- 文章列表 -->
        <section class="article-list">
          <h3 class="section-title">最新文章</h3>
          <div class="articles-container">
            <article v-for="article in articles" :key="article.id" class="article-card">
              <img v-if="article.coverImg !== null" :src="getImageUrl(article.coverImg)" class="article-image">
              <div v-if="article.coverImg === null" :style="{ 'background-color': getRandomColor() }" class="article-image"></div>
              <div class="article-content">
                <h4 class="article-title">{{ article.title }}</h4>
                <p class="article-excerpt">{{ article.excerpt }}</p>
                <div class="article-meta">
                  <span class="article-date">{{ article.createTime }}</span>
                  <span class="article-category">{{ article.tagCodes===undefined?'':article.tagCodes[0] }}</span>
                </div>
                <router-link :to="'/article/' + article.id" class="read-more">阅读更多</router-link>
              </div>
            </article>
          </div>
        </section>
      </div>
    </main>
    <footer class="footer">
      <div class="detail">
        <p>{{about.copyright}}</p>
      </div>
    </footer>
</template>

<script setup name="Cards">
import { ref,reactive, onMounted } from 'vue';
import { getNews } from "@/api/cards"
import { useRouter  } from 'vue-router'
import { getImageUrl } from '@/utils/imageHelp'
import { getAbout } from "@/api/home"
const router = useRouter ()
const articles = ref([]);

const menuActive = ref(false);
const isMobile = ref(false);
const about = ref({});
const getRandomColor = () => {
  const colors = [
    '#E1F5FE', '#B3E5FC', '#81D4FA', '#4FC3F7', '#29B6F6',
    '#03A9F4', '#039BE5', '#0288D1', '#0277BD', '#01579B'
  ];
  return colors[Math.floor(Math.random() * colors.length)];
};

const toggleMenu = () => {
  menuActive.value = !menuActive.value;
};

const closeMenu = () => {
  menuActive.value = false;
};

const checkScreenSize = () => {
  isMobile.value = window.innerWidth <= 768;
  // 如果屏幕大于768px，确保菜单是展开状态
  if (!isMobile.value) {
    menuActive.value = false;
  }
};

const getList = () => {
  getNews().then(response => {
      articles.value = response.data
  })
}

const selectAbout = () => {
    let aboutObj = JSON.parse(localStorage.getItem('about'))
    if(aboutObj){
        about.value = aboutObj
        return false
    }
    getAbout().then(response => {
        about.value = response.data
    })
}

onMounted(() => {
  selectAbout()
  checkScreenSize();
  window.addEventListener('resize', checkScreenSize);
  getList()
});

    // return {
    //   articles,
    //   menuActive,
    //   getRandomColor,
    //   toggleMenu,
    //   closeMenu
    // };
</script>

<style scoped>
/* 主要内容样式 */
.main-content {
  padding: 5rem 0;
  background-color: var(--white);
}

.welcome-banner {
  text-align: center;
  padding: 3rem 0;
  background-color: #333;
  background-size: cover;
  border-radius: 8px;
  margin-bottom: 2rem;
}

.welcome-banner h2 {
  font-size: clamp(1.8rem, 4vw, 2.5rem);
  margin-bottom: 1rem;
  color: #fff;
}

.welcome-banner p {
  font-size: clamp(1rem, 2vw, 1.2rem);
  color: #fff;
}

.section-title {
  font-size: clamp(1.5rem, 3vw, 1.8rem);
  margin-bottom: 1.5rem;
  color: var(--accent-color);
  border-bottom: 2px solid #000;
  padding-bottom: 0.5rem;
}

/* 筛选区域 */
.filter-section {
  background-color: var(--white);
  padding: 1.5rem 0;
  box-shadow: var(--shadow-sm);
}

.search-filter-container {
  width: 50%;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
}

.search-box {
  flex: 1;
  min-width: 50px;
  display: flex;
}

.search-input {
  flex: 1;
  padding: 0.75rem 1rem;
  border: 2px solid var(--secondary-blue);
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  font-size: 1rem;
  outline: none;
  transition: all 0.3s ease;
}

/* .search-input:focus {
  border-color: var(--primary-black);
  box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.2);
} */

.search-button {
  padding: 0 1.25rem;
  background-color: var(--primary-blue);
  color: var(--white);
  border: none;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.search-button:hover {
  background-color: var(--primary-dark);
}

/* 文章列表样式 */
.articles-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.article-card {
  background: var(--white);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform 0.3s, box-shadow 0.3s;
}

.article-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.article-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  height: 180px;
}

.article-content {
  padding: 1.5rem;
}

.article-title {
  margin-top: 0;
  margin-bottom: 0.5rem;
  font-size: 1.3rem;
  text-align: left;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 限制显示行数 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.article-excerpt {
  color: var(--light-text);
  margin-bottom: 1rem;
  text-align: left;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 限制显示行数 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  margin-bottom: 1rem;
  color: var(--light-text);
}

.read-more {
  display: inline-block;
  padding: 0.5rem 1rem;
  background-color: var(--accent-color);
  color: var(--white);
  text-decoration: none;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.read-more:hover {
  color: #4CAF50;
}

.social-links {
  margin-top: 1rem;
}

.social-icon {
  margin: 0 0.5rem;
  color: var(--text-color);
  text-decoration: none;
  transition: color 0.3s;
}

.social-icon:hover {
  color: var(--accent-color);
}

.footer {
    margin-top: auto;
    border-top: #333 solid 1px;
    color: #333;
    padding: 15px 0;
}

/* 确保PC端初始正确显示 - 重要修改 */
@media (min-width: 768px) {
  .articles-container {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* 中等屏幕布局 */
@media (min-width: 768px) and (max-width: 1023px) {
  .articles-container {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>